package com.deyuanyun.pic.ctrl.service.pushmsg;

import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.ctrl.task.OclPointNoticeTask;
import com.deyuanyun.pic.settings.controller.dto.BizMsgThresholdVO;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.enumeration.ThresholdType;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.MsgQueue;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;
import com.google.common.collect.Lists;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *  外涂层漏损点消息推送
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-10-25
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class OclPointNoticePushMsg extends AbstractPushMsgTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(OclPointNoticePushMsg.class);

    @Override
    public void doPushMsgData() {
        if(super.interfaceParam.isEmpty()) {
            LOGGER.info("没有传入参数");
            return;
        }
        Map<String, Object> interfaceMap = super.interfaceParam;
        int day = Integer.valueOf(interfaceMap.get(OclPointNoticeTask.DAY_KEY).toString());// 提前通知天数
        String pipeLineId = (String)interfaceMap.get(OclPointNoticeTask.PIPE_LINE_ID_KEY);//管道id
        String customNumber = (String) interfaceMap.get(OclPointNoticeTask.CUSTOM_NUMBER);//漏损点编号
        String locationDesc = (String)interfaceMap.get(OclPointNoticeTask.LOCATION_DESCRIPTION); //位置描述
        LocalDate localDate = (LocalDate)interfaceMap.get(OclPointNoticeTask.DATE_KEY); //具体时间

        BizMsgThresholdVO msgThresholdVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.INTERNAL_PROTECTING_COATING_ADVISE_INPUT.getValue());

        if(msgThresholdVO == null) {
            LOGGER.info("没有找到对应的设置项");
            return;
        }
        String pipeLineName = PipeDictUtils.getLineById(pipeLineId).getName();
        super.thresListCastString(Lists.newArrayList(pipeLineName, locationDesc, customNumber, localDate.toString("yyyy-MM-dd")));
        initializeBizMsgSends(msgThresholdVO);
    }

    @Override
    public void initializeBizMsgSends(Object... args) {
        BizMsgThresholdVO msgThresholdVO = (BizMsgThresholdVO) args[0];
        super.roleList = msgThresholdVO.getRoleIdList();

        if(super.roleList != null && !super.roleList.isEmpty()) {
            Set<String> userIds = super.bizMsgThresholdService.queryUseIdByRoleIds(super.roleList); //用户id集合
            List<MsgQueue.BizMsgUserInfo> userInfos = new ArrayList<MsgQueue.BizMsgUserInfo>();
            for (String userId : userIds) {
                LOGGER.info("开始向用户id为：" + userId + "的用户推送消息");
                BizMsgUser bizMsgUser = new BizMsgUser();

                if (StringUtils.isNotEmpty(super.thresholdContent)) {
                    bizMsgUser.setThresholdContent(super.thresholdContent);
                }
                bizMsgUser.setId(CommonUtil.getUUID());
                bizMsgUser.setMsgContent(msgThresholdVO.getMsgContent());
                bizMsgUser.setUserId(userId);
                bizMsgUser.setCreateDate(new Date());
                bizMsgUser.setCreateRemark("防腐层漏损点最迟维修消息推送");

                userInfos.add(msgQueue.new BizMsgUserInfo().setValid(false).setBizMsgUser(bizMsgUser));
            }
            if(!userInfos.isEmpty()) {
                MsgQueue.MsgInfo msgInfo = msgQueue.new MsgInfo().setBizMsgThresholdId(msgThresholdVO.getId())
                        .setThresholdSet(msgThresholdVO.getThresholdContent()).setRoleIds(super.roleList);
                msgQueue.addOperator(msgQueue.new PushMsgOperator(userInfos, msgInfo));
            }
        }
    }

    @Override
    public boolean valiIsPush(Map<String, Object> valiMap) {
        return false;
    }
}
